var stuinfos = [];
var upstuinfos = [];
$(function(){
	//视频播放
	scoredetail_video();
	//见面课信息
	$.ajax({
		   type: "POST",
		   url: basePath + "/operation/scoredetail/findMeetCourseById",
		   data:{id:$("#taskId").val()},
		   dataType:"json",
		   success: function(map){
			 var data=map.teachingTaskDto;
			$("#teachRequire_textarea").text( map.teachRequire.replace(/<.*?>/ig,""));
		  $("#facetoface_title").text(data.taskName);
		  $("#facetoface_title").attr("title",data.taskName);
		   $("#speakerStr").text(data.speakerStr);
		   $("#speakerStr").attr("title",data.speakerStr);
		   $("#execiteTime").text(new Date(data.execiteTime).Format("yyyy-MM-dd"));
		   $("#video_url").attr("value",data.videoId);
		   $("#checkScore").text(map.checkScore==null?0:map.checkScore);
		   $("#siteScore").text(map.siteScore==null?0:map.siteScore);
		   $("#sceneScore_score").attr("maxlength",$("#siteScore").text().length);
		   }
		   });
	//加载学生信息
	loadstuinfo();
	//限制打分最大数
$("#sceneScore_score").live("keyup",function(){
	scoreValidation(this,0,$("#siteScore").text());
});
//显示教学要求
$("#teachingrequirements_div").live("click",function(){
	 layer.open({
		  type:1,
		  shade: false,
		  title :zLocale.fourstep_jiaoxueyaoqiu,
		  area:[600,500],
		  fix: false, //不固定
		  content:$("#teachRequire"),
		  btn: [zLocale.public_confim] //按钮  
		});
	
});
	//查询
	$("#queryStuInfo").live("click",function(){
		queryStuInfo();
	});
	//弹窗批量签到窗口
	$("#batch_Attendance").live("click",function(){
          if(selectedStuSum()==0){
			layer.open({
				title:" ",
				icon:0,
				content:zLocale.scoredetail_zhishaoyige,
				shade:.6,
				skin: 'layui-layer-zhs',
				btn: [zLocale.public_confim] //按钮  
			});
			return false;
		}
		showDetail("bgDiv_zz");
		$("[name=sum]").text(selectedStuSum());
     $("#batch_Attendance_tooltipbox").show();
	});
	//弹窗批量打分窗口
	$("#bacth_Performance").live("click",function(){
		if(selectedStuSum()==0){
			layer.open({
				title:" ",
				icon:0,
				content:zLocale.scoredetail_zhishaoyige,
				shade:.6,
				skin: 'layui-layer-zhs',
				btn: [zLocale.public_confim] //按钮  
			});
			return false;
		}
		showDetail("bgDiv_zz");
		 $("[name=sum]").text(selectedStuSum());
		$("#bacth_Performance_tooltipbox").show();
	});
	//阻止复选框事件冲突
	$("[type=checkbox]").live("click",function(e){
	    e.stopPropagation();   
	});
	$("[name=a]").live("click",function(){
	if($(this).parent("tr").find("td").eq(0).find("input").attr("checked")==null){
		$(this).parent("tr").find("td").eq(0).find("input").attr("checked","checked");
	}else{
		$(this).parent("tr").find("td").eq(0).find("input").removeAttr("checked")
	};
	});

	//单个签到
	$("td .attendancediv").live("click",function(){
		var userId=$(this).parent("td").parent("tr").find("td").eq(2).text();
		var obj=$(this).find("span");
		if(obj.attr("class")=="attendanceicoclose"){
			obj.val(1);
			obj.addClass("attendanceicoopen");
			obj.removeClass("attendanceicoclose");
		}else{
			obj.val(0);
			obj.addClass("attendanceicoclose");
			obj.removeClass("attendanceicoopen");
		}
		upstu(userId,obj.val(),"signStatus");
		$.ajax({
			   type: "POST",
			   url: basePath + "/operation/scoredetail/upOrSaveStu",
			   data:{"upStuInfoString":JSON.stringify(upstuinfos)},
			   dataType:"json",
			   success: function(map){
				   scoressavedhint_div_show(); 
			   }
			   });
		
	});
	//单个打分
	$("[name=sceneScore]").live("blur",function(){
		var userId=$(this).parent("div").parent("td").parent("tr").find("td").eq(2).text();
		
		for(var key in stuinfos){
			if(stuinfos[key].userId==userId){
				if(stuinfos[key].sceneScore==$(this).val()||$(this).val()==null || $(this).val()=="null")
				return;
		}
		}	
		upstu(userId,$(this).val(),"sceneScore");
		$.ajax({
			   type: "POST",
			   url: basePath + "/operation/scoredetail/upOrSaveStu",
			   data:{"upStuInfoString":JSON.stringify(upstuinfos)},
			   dataType:"json",
			   success: function(map){
					
				   scoressavedhint_div_show();
			   }
			   });
		
	});
	
	
	//全选\反选
	
	$("#allchecked").change(function(){
		if($(this).attr("checked")=="checked"){
			$("[type=checkbox]").attr("checked","checked");
			}else{
		   $("[type=checkbox]").removeAttr("checked")
			}
	});
});


//获取勾选人数
function  selectedStuSum(){
	var checkeds=$("td .checkall_div").find("[type=checkbox]");
	var sumchecked=0;//勾选人数
	checkeds.each(function(i){
		if($(this).attr("checked")=="checked"){
			sumchecked++
			}
	});
	return sumchecked;
}
//获取勾选学生详情
function  selectedStuInfo(){
	var arrinfo = [];
	var checkeds=$("td .checkall_div").find("[type=checkbox]");
	checkeds.each(function(i){
		var arrinfoobj= {};
		if($(this).attr("checked")=="checked"){
			var userId= $(this).parent("div").parent("td").parent("tr").find("td").eq(2).text();
			var signStatus= $(this).parent("div").parent("td").parent("tr").find("td").eq(4).find("div").find("span").attr("value");
			var sceneScore= $(this).parent("div").parent("td").parent("tr").find("td").eq(5).find("input").val();
			var upstay= $(this).parent("div").parent("td").parent("tr").find("td").eq(6).text();
			var classId= $(this).parent("div").parent("td").parent("tr").find("td").eq(7).text();
			var realName= $(this).parent("div").parent("td").parent("tr").find("td").eq(3).text();
			var id= $(this).parent("div").parent("td").parent("tr").find("td").eq(8).text();
			arrinfoobj["userId"]=userId;
			arrinfoobj["signStatus"]=signStatus;
			arrinfoobj["sceneScore"]=sceneScore;
			arrinfoobj["upstay"]=upstay;
			arrinfoobj["classId"]=classId;
			arrinfoobj["taskId"]=$("#taskId").val();
			arrinfoobj["courseId"]=$("#courseId").val();
			arrinfoobj["recruitId"]=$("#recruitId").val();
			arrinfoobj["realName"]=realName;
			arrinfoobj["id"]=id;
			arrinfo.push(arrinfoobj);
		}
	});
	return arrinfo;
}

//批量签到操作
function batchAttendance(type){ //type :1 签到    0：取消签到
	
    var userSigninDtosString=[];
	var checkeds=$("td .checkall_div").find("[type=checkbox]");
	checkeds.each(function(i){
		if($(this).attr("checked")=="checked"){
			var sceneScore= $(this).parent("div").parent("td").parent("tr").find("td").eq(4).find("span");
			if(type=="1"){
				sceneScore.attr("value",1);
				sceneScore.addClass("attendanceicoopen");
				sceneScore.removeClass("attendanceicoclose");	
			}else{
				sceneScore.attr("value",0);
				sceneScore.addClass("attendanceicoclose");
				sceneScore.removeClass("attendanceicoopen");
			}
			}
		
	});
	upstus(selectedStuInfo(),"signStatus");
	$.ajax({
		   type: "POST",
		   url: basePath + "/operation/scoredetail/upOrSaveStu",
		   data:{"upStuInfoString":JSON.stringify(upstuinfos),
		    "isUp":1},
		   dataType:"json",
		   success: function(map){
				
			   scoressavedhint_div_show();	
		   }
		   });
	//隐藏弹窗
	hidetooltipbox();
}
//批量打分操作
function bacthPerformance(){ 
	
	if($("#sceneScore_score").val())
	
	var checkeds=$("td .checkall_div").find("[type=checkbox]");
	checkeds.each(function(i){
		if($(this).attr("checked")=="checked"){
			var input= $(this).parent("div").parent("td").parent("tr").find("td").eq(5).find("input");
			input.val($("#sceneScore_score").val());
			
			}
	});
	upstus(selectedStuInfo(),"sceneScore");
	$.ajax({
		   type: "POST",
		   url: basePath + "/operation/scoredetail/upOrSaveStu",
		   data:{"upStuInfoString":JSON.stringify(upstuinfos)},
		   dataType:"json",
		   success: function(map){
				
			   scoressavedhint_div_show(); 
		   }
		   });
	//隐藏弹窗
	hidetooltipbox();
	
}


//批量修改学生集合
function upstus(objs,property){
	for(var key in stuinfos){
		for(var i in objs){
			   if(stuinfos[key].userId==objs[i].userId){
				   stuinfos[key][property]=objs[i][property];
				   upstuinfos=objs;
			   };
			 }
		 }
}
//单个修改学生集合
function upstu(userId,upvalue,property){
	for(var key in stuinfos){
			if(stuinfos[key].userId==userId){
				stuinfos[key][property]=upvalue;
				upstuinfos=stuinfos[key];
		}
	}
}


//显示学生考勤信息
function showstuInfo(arrInfo){
	for(var i=0;i<arrInfo.length;i++){
		var type=arrInfo[i].signStatus==0?"attendanceicoclose":"attendanceicoopen";
		var sceneScore=arrInfo[i].sceneScore==null?"":arrInfo[i].sceneScore;
		var html=' <tr>'
			   + '<td name="a"><div class="checkall_div"><input name="" type="checkbox" value=""></div></td>'
			   + '<td  name="a"><div class="headportrait_div"><img src="'+arrInfo[i].imgurl+'" width="40" height="40"></div></td>'
			   + ' <td  name="a" class="userId" style="display:none;">'+arrInfo[i].userId+'</td>'
			   + ' <td  name="a"><div class="namediv" title="'+arrInfo[i].realName+'">'+arrInfo[i].realName+'</div></td>'
			   + ' <td><div class="attendancediv"><span class="'+type+'"  value="'+arrInfo[i].signStatus+'"><span class="button"></span></span></div></td>'
			   +  ' <td><div class="performance"><input onpaste="return false" name="sceneScore" maxlength="'+ $("#siteScore").text().length+'" type="text" data-tags="'+zLocale.scoredetail_biaoxian+'"  onkeyup="scoreValidation(this,0,'+$("#siteScore").text()+')" onchange="scoreValidation(this,0,'+$("#siteScore").text()+')"   class="getFocus" value="'+sceneScore+'"  placeholder="'+zLocale.scoredetail_biaoxian+'" /></div></td>'
			   +  ' <td class="upstay"  style="display:none;">'+arrInfo[i].upstay+'</td>'
			   +  ' <td class="class_id"  style="display:none;">'+arrInfo[i].classId+'</td>'
			   +  ' <td class="qiandao_id"  style="display:none;">'+arrInfo[i].id+'</td>'
			   +  ' </tr>';
		//左右显示
		if(i%2==0){
			$("#leftInfo").append(html);
		}else{
			$("#rightInfo").append(html);
		}
	}
	/*if(arrInfo.length%2!=0){
		//单数 清空最后一个
		//$("#rightInfo").append(html);
		//$("#rightInfo").find("tr").eq(parseInt(arrInfo.length/2)).find("div").html("");
	}*/
	
	
}
//根据姓名查询  为空显示全部
function querybyName(realName,arrInfo){
	if(realName=="Please enter a student′s name." || realName=="请输入学生的姓名"){
		 $("#rightInfo").html(""); 
  	   $("#leftInfo").html("");
		 showstuInfo(arrInfo);
		return;
	}
	var arr=[];
	       for(var i = 0; i < arrInfo.length; i++){
	           var obj = arrInfo[i];
	               if(obj["realName"].indexOf(realName) >= 0 || obj["realName"]==realName){
	            	   $("#rightInfo").html(""); 
	            	   $("#leftInfo").html("");
	            	   arr.push(obj);
	           }
	       }
	       showstuInfo(arr);
}

//隐藏弹窗
function hidetooltipbox(){
	hideDetail("bgDiv_zz");
	$("[type=checkbox]").removeAttr("checked");
	   $("#sceneScore_score").val("");
		$("#bacth_Performance_tooltipbox").hide();
	    $("#batch_Attendance_tooltipbox").hide();
	 
}
//显示保存成功 后两秒后隐藏
	function scoressavedhint_div_show(){
		$("#scoressavedhint_div").show();
		setTimeout('$("#scoressavedhint_div").fadeOut()',1000);
	}
	
	//扩展时间格式方法
	Date.prototype.Format = function (fmt) { //author: meizz 
	    var o = {
	        "M+": this.getMonth() + 1, //月份 
	        "d+": this.getDate(), //日 
	        "h+": this.getHours(), //小时 
	        "m+": this.getMinutes(), //分 
	        "s+": this.getSeconds(), //秒 
	        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
	        "S": this.getMilliseconds() //毫秒 
	    };
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	}
	//初始化学生信息
	function loadstuinfo(){
		var dataparm={"recruitId":$("#recruitId").val(),
			"taskId":$("#taskId").val(),	
			"courseId":$("#courseId").val()	
		};
		
		$.ajax({
			   type: "POST",
			   url: basePath + "/operation/scoredetail/stuInfo",
			   data:dataparm,
			   dataType:"json",
			   success: function(datas){
				   stuinfos=datas;
				   showstuInfo(datas);
				   
			   }
		});
	}
	//查询
	function  queryStuInfo(){
		var judgeCheck=false;
		var queryname=$("#queryName").val();
		  for(var i = 0; i < stuinfos.length; i++){
			  if(stuinfos[i].realName.indexOf(queryname) >= 0||stuinfos[i].realName==queryname ||queryname=="Please enter a student′s name." || queryname=="请输入学生的姓名"){
					$("#stuInfo").html(' <tr  class="clearfix"><td class="fl"><table id="leftInfo" width="580" border="0" cellspacing="0" cellpadding="0" class=""></table></td><td class=""><table id="rightInfo" widtd="580" border="0" cellspacing="0" cellpadding="0"></table></td></tr>');
				  querybyName(queryname,stuinfos);
				  judgeCheck= true;
			  }
		  }
		if(!judgeCheck){
			$("#stuInfo").html(" <tr><td style='height:140px;color: #999;text-align:center'>"+zLocale.scoredetail_wuxuesheng+" </td></tr>");
			return;
		}
	}
	 /**
	  * 按下键盘事件
	  * 当按下回车时，查询
	  */	
	 $(window).keydown(function(event){
	   if (event.keyCode==13){    
		   queryStuInfo();
	   }  
	 });